package utils

import (
	"context"
	"time"

	"go.uber.org/zap"
	gormlog "gorm.io/gorm/logger"
)

type CustomGORMLogger struct {
	Clog     *zap.Logger
	TraceSql bool
}

func (c *CustomGORMLogger) LogMode(gormlog.LogLevel) gormlog.Interface {
	return c
}
func (c *CustomGORMLogger) Info(ctx context.Context, str string, ext ...interface{}) {
	c.Clog.Info(str, zap.Any("ext", ext))
}
func (c *CustomGORMLogger) Warn(ctx context.Context, str string, ext ...interface{}) {
	c.Clog.Warn(str, zap.Any("ext", ext))
}
func (c *CustomGORMLogger) Error(ctx context.Context, str string, ext ...interface{}) {
	c.Clog.Error(str, zap.Any("ext", ext))
}
func (c *CustomGORMLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error) {
	if c.TraceSql == false {
		return
	}
	sql, rowsAffected := fc()
	c.Clog.Debug("SQL Trace", zap.String("sql", sql), zap.Int64("rowsAffected", rowsAffected), zap.Error(err))
}
